//
//  NearbyCityVC.m
//  ImGuider
//
//  Created by 王鹏宇 on 2017/12/26.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "NearbyCityVC.h"
#import "DestinationCell.h"
#import "CityVC.h"
@interface NearbyCityVC ()<UITableViewDataSource,UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong)NSMutableArray * dataSource;

@end

@implementation NearbyCityVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setupUI];
}

- (void)setupUI {
    

    [self setAutoRowHeight:self.tableView];
    [self registCells:@[@"DestinationCell"] tableView:self.tableView];
    
    if (!self.GPSCity) {
        
        return;
    }
     [ActivityIndicatorView showHUDAddedTo:self.view];
    if (![CountryManager sharedManager].onlinecountry) {
        [ActivityIndicatorView showHUDAddedTo:self.view];
        @weakify(self);
        [NetAPIManager getOnlinecitiesWithBlock:^(NSArray *onlinecountry, NSError *error) {
            @strongify(self);
            [CountryManager sharedManager].onlinecountry = onlinecountry;
            [self loadDataSource];
        }];
        
        return;
    }
    
    [self loadDataSource];
}

- (void)loadDataSource {
    
    @weakify(self);
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        NSArray *cityArray = [[CountryManager sharedManager] getNearbyCity:self.GPSCity];
        
        dispatch_async(dispatch_get_main_queue(), ^ {
            
            @strongify(self);
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            self.dataSource = [NSMutableArray arrayWithArray:cityArray];
            
            [self.tableView reloadData];
        });
    });
}


- (void)scanAction {
    
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DestinationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DestinationCell"];
    
    City * city = self.dataSource[indexPath.row];
    cell.city = city;
    
    return cell;
}


#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    City *city = self.dataSource[indexPath.row];
    
    CityVC *cityVC = [CityVC new];
    cityVC.cid = city.cid.stringValue;
    
    [self.superVC.navigationController pushViewController:cityVC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
